package dao;

import javax.persistence.CacheRetrieveMode;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.*;
import util.EntityManagerUtil;

public class HorderDao {
	public static Horder newHorder(String temail, String hemail) throws Exception {
		Hotel houtel = HotelDao.getHotelNoCache(hemail);
		Tourist tourist = TouristDao.getTouristByEmail(temail);
		Horder horder = new Horder();
		HorderPK pk = new HorderPK();
		pk.setHEmail(houtel.getEmail());
		pk.setTEmail(tourist.getEmail());
		horder.setId(pk);
		horder.setMoney(houtel.getPrice());
		return horder;
	}

	public static Horder getHorder(Horder horder) {
		EntityManager em = EntityManagerUtil.getEntityManager();
		Horder horderFind = em.find(Horder.class, horder.getId());
		if (horderFind != null) {
			em.close();
			return horderFind;
		} else
			return null;
	}

	public static Horder getHorderNoCache(Horder horder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Horder c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Horder horderFind = (Horder) query.setParameter("id", horder.getId()).getSingleResult();
		em.close();
		if (horderFind != null)
			return horderFind;
		else
			return null;
	}

	public static boolean getHorderNoCacheForBool(Horder horder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		String sql = "select c from Horder c where c.id=:id";
		Query query = em.createQuery(sql);
		query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.BYPASS);
		Horder horderFind;
		try {
			horderFind = (Horder) query.setParameter("id", horder.getId()).getSingleResult();
			em.close();
			return true;
		} catch (Exception e) {
			em.close();
			return false;
		}

	}
	public static boolean updateHorderStatus(Horder horder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Horder horderFind = getHorderNoCache(horder);
			if (horderFind != null) {
				Horder upHorder = em.find(Horder.class, horder.getId());
				em.getTransaction().begin();
				upHorder.setStatus(horder.getStatus());
				em.persist(upHorder);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}
	public static boolean addHorder(Horder horder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Horder horderFind = getHorderNoCache(horder);
		} catch (Exception e) {
			em.getTransaction().begin();
			Hotel hotel = HotelDao.getHotelNoCache(horder.getId().getHEmail());
			hotel.setSum(hotel.getSum() - 1);
			//向数据库插入数据
			em.merge(hotel);
			em.persist(horder);
			em.getTransaction().commit();
			em.close();
			return true;
		}
		return false;
	}

	public static boolean updateHorder(Horder horder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Horder horderFind = getHorderNoCache(horder);
			if (horderFind != null) {
				Horder upHorder = em.find(Horder.class, horder.getId());
				em.getTransaction().begin();
				int money = HotelDao.getHotel(getHorderNoCache(horder).getHotel()).getPrice();
				upHorder.setMoney(money);
				em.persist(upHorder);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static boolean delHorder(Horder horder) throws Exception {
		EntityManager em = EntityManagerUtil.getEntityManager();
		try {
			Horder horderFind = getHorderNoCache(horder);
			if (horderFind != null) {
				Horder delHorder = em.find(Horder.class, horder.getId());
				em.getTransaction().begin();
				Hotel hotel = HotelDao.getHotelNoCache(horder.getId().getHEmail());
				hotel.setSum(hotel.getSum() + 1);
				em.merge(hotel);
				em.remove(delHorder);
				em.getTransaction().commit();
				em.close();
				return true;
			} else {
				return false;
			}
		} catch (Exception e) {
			return false;
		}

	}

	public static void main(String args[]) throws Exception {
	}

}
